import {
  writeOffList,
  orderDetails
} from '../../apis/user';
import {
  formatTime,
  handleDate
} from '../../utils/util';
const app = getApp();

Page({
  data: {
    list: [],
    pageNum: 1,
    isRequest: false,
    isHaveMore: true,
    popupShow: false,
    details: {}
  },

  onLoad() {
    this.getwriteOffList(1);
  },

  onReachBottom() {
    if (this.data.isHaveMore && !this.data.isRequest) {
      this.getwriteOffList(this.data.pageNum + 1);
    };
  },

  // 获取记录
  getwriteOffList(pageNum, type) {
    if (this.data.isRequest) return
    this.setData({
      isRequest: true
    });
    writeOffList(pageNum, 10).then(res => {
      this.setData({
        isRequest: false
      });
      if (res.data.code === 200) {
        this.setData({
          pageNum: pageNum
        });
        let {
          rows,
          total
        } = res.data;
        rows.forEach(v => {
          v.createTime = formatTime(v.createTime);
        });
        if (type === 'load') {
          rows.forEach(v => {
            this.data.list.push(v);
          });
          this.setData({
            list: this.data.list
          });
        } else {
          this.setData({
            list: rows
          });
        };

        if (this.data.list.length === total) {
          this.setData({
            isHaveMore: false
          });
        };
      };
    });
  },

  // 获取详情
  getVerifierActivity(order) {
    wx.showLoading({
      title: '请稍候...',
      mask: true
    });
    orderDetails(order).then(res => {
      wx.hideLoading();
      if (res.data.code === 200) {
        let {
          orderDetail
        } = res.data.data;
        orderDetail.startTime = handleDate(orderDetail.startTime);
        orderDetail.closeTime = handleDate(orderDetail.closeTime);
        orderDetail.sceneStatus = res.data.data.sceneStatus;
        this.setData({
          details: orderDetail,
          popupShow: true
        });
      };
    });
  },

  // 查看记录
  lookRecord(e) {
    let {
      order
    } = e.currentTarget.dataset;
    this.getVerifierActivity(order);
  },

  hidePopup() {
    this.setData({
      popupShow: false
    });
  }
})